/* *  
 * Copyright (C) 2004-2008 Mocha Almond Fudge AI, SIT KMUTT
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package jboard.util;


import java.util.Random;
import java.util.Vector;

import jboard.model.BoardPosition;
import jboard.model.IBoardConstant;

/**
 * 
 * @author Vashira Ravipanich
 */
public class Utilities {
    
    public static int getCurrentTurn(boolean isAITurn) {
		int currentTurn;
		// check turn
		if(isAITurn)
		    currentTurn = IBoardConstant.BLACK;
		else
		    currentTurn = IBoardConstant.WHITE;
		return currentTurn;
    }
    
    public static int getNoToken(BoardPosition boardPosition, int color) {
    	int count = 0;
    	for(int x = 0; x < 8; x++) {
			for(int y = 0; y < 8; y++) {
			    if(boardPosition.peek(x, y) == color) count++;
			}
    	}
    	return count;
    }
    
    /**
     * Initial Token on the given BoardPosition
     * @param boardPosition
     */
    public static void initialTokenOnBPS(BoardPosition boardPosition, boolean isReverse) {
        for(int x = 0; x < 8; x++) {
            for(int y = 0; y < 8; y++) {
                if(!isReverse) {	// Normal Order (Player in lower section, Ai in upper section)
                    if(y == 0) 
                        boardPosition.addToken(x, y, IBoardConstant.BLACK);
                    else if(y == 7)
                        boardPosition.addToken(x, y, IBoardConstant.WHITE);
                } else {	// Reverse Order (Player in upper section, Ai in lower section)
                    if(y == 0) 
                        boardPosition.addToken(x, y, IBoardConstant.WHITE);
                    else if(y == 7)
                        boardPosition.addToken(x, y, IBoardConstant.BLACK);
                }
            }
        }
    }
    
    /**
     * CHECKING: all avilable PositionAI's strategy
     * @param boardPosition
     * @param strategy
     */
    public static void initTokenStrategy(BoardPosition boardPosition, boolean isReverse, int strategy) {
        if(!isReverse) {	// Normal order
            switch (strategy) {
            case IBoardConstant.STAIR_LEFT_FORMAT:
	            boardPosition.addToken(0, 0, IBoardConstant.BLACK);
	            boardPosition.addToken(1, 1, IBoardConstant.BLACK);
	            boardPosition.addToken(2, 2, IBoardConstant.BLACK);
	            boardPosition.addToken(3, 3, IBoardConstant.BLACK);
	            boardPosition.addToken(4, 4, IBoardConstant.BLACK);
	            boardPosition.addToken(5, 5, IBoardConstant.BLACK);
	            boardPosition.addToken(6, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(7, 6, IBoardConstant.BLACK);
            break;
            case IBoardConstant.STAIR_RIGHT_FORMAT: 
                boardPosition.addToken(7, 0, IBoardConstant.BLACK);
	            boardPosition.addToken(6, 1, IBoardConstant.BLACK);
	            boardPosition.addToken(5, 2, IBoardConstant.BLACK);
	            boardPosition.addToken(4, 3, IBoardConstant.BLACK);
	            boardPosition.addToken(3, 4, IBoardConstant.BLACK);
	            boardPosition.addToken(2, 5, IBoardConstant.BLACK);
	            boardPosition.addToken(1, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(0, 6, IBoardConstant.BLACK);
            break;
            case IBoardConstant.FLAT_FORMAT: 
                boardPosition.addToken(0, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(1, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(2, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(3, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(4, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(5, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(6, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(7, 6, IBoardConstant.BLACK);
            break;
            default:
                break;
            }
        } else { // Reverse Order
            switch (strategy) {
            case IBoardConstant.STAIR_LEFT_FORMAT:
                boardPosition.addToken(0, 1, IBoardConstant.BLACK);
	            boardPosition.addToken(1, 1, IBoardConstant.BLACK);
	            boardPosition.addToken(2, 2, IBoardConstant.BLACK);
	            boardPosition.addToken(3, 3, IBoardConstant.BLACK);
	            boardPosition.addToken(4, 4, IBoardConstant.BLACK);
	            boardPosition.addToken(5, 5, IBoardConstant.BLACK);
	            boardPosition.addToken(6, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(7, 7, IBoardConstant.BLACK);
            break;
            case IBoardConstant.STAIR_RIGHT_FORMAT: 
                boardPosition.addToken(7, 0, IBoardConstant.BLACK);
	            boardPosition.addToken(6, 1, IBoardConstant.BLACK);
	            boardPosition.addToken(5, 2, IBoardConstant.BLACK);
	            boardPosition.addToken(4, 3, IBoardConstant.BLACK);
	            boardPosition.addToken(3, 4, IBoardConstant.BLACK);
	            boardPosition.addToken(2, 5, IBoardConstant.BLACK);
	            boardPosition.addToken(1, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(0, 6, IBoardConstant.BLACK);
            break;
            case IBoardConstant.FLAT_FORMAT: 
                boardPosition.addToken(0, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(1, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(2, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(3, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(4, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(5, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(6, 6, IBoardConstant.BLACK);
	            boardPosition.addToken(7, 6, IBoardConstant.BLACK);
            break;
            default:
                break;
            }
        }
    }
    
    public static int oppositeColor(int color) {
    	switch(color) {
			case IBoardConstant.BLACK:
				return IBoardConstant.WHITE;
			case IBoardConstant.WHITE:
				return IBoardConstant.BLACK;
    	}
    	return IBoardConstant.EMPTY;
    }
    
    public static boolean passTurn(boolean isAITurn) {
        return !isAITurn;
    }
    
    /**
     * PERFORMANCE is BigO N2
     * @param base
     * @return
     */
    public static Vector shuffle(Vector base) {
    	long timeSeed = System.currentTimeMillis();
    	Random r = new Random(timeSeed);
    	Object tmpCurrent;
    	int rand;
    	
    	final int shuffTime = Math.abs(r.nextInt()) % 5;
    	
    	for(int i = 0; i < shuffTime; i++) {
	    	for (int a = 0; a < base.size(); a++) {
	    		rand = (Math.abs(a + r.nextInt(base.size())) % base.size());
	    		Object toAdd = base.remove(rand);
	    		base.add(toAdd);
	    	}
    	}
    	return base;
    }
}
